//
//  TMUserIconController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/28.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMUserIconController.h"
#import "TMUserViewModel.h"
#import "NSString+TMNotNull.h"
#import "TMChoicePhotosView.h"
#import "TMUtils.h"
@interface TMUserIconController ()<UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *bindView;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *discountCouponView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *phoneViewTopConst;//280+64 13
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *discountViewTopConst;//353+64  33
@property (weak, nonatomic) IBOutlet UIImageView *validityImageV;
@property (weak, nonatomic) IBOutlet UILabel *courseLabel;
@property (weak, nonatomic) IBOutlet UILabel *overDateTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UIImageView *useRightView;
@property (weak, nonatomic) IBOutlet UITextField *userNameTf;
@property (weak, nonatomic) IBOutlet UITextField *wecheAccountTf;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, assign) ChoosePhotoIV types;
@property (nonatomic, strong) UIImageView *imagev;
@property (nonatomic, copy) NSString *iconUrls;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@end

@implementation TMUserIconController
- (IBAction)codeAction:(UIButton *)sender {
    if (self.userNameTf.text.length != 11 || ![self inputShouldNumber:self.userNameTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入正确的手机号码！"];
        return;
    }else{
        [self.codeBtn startTimer:60 andKey:@"keys"];
        [TMUserManager verificationCodeSendWithDictionary:self.userNameTf.text];
    }
}
#pragma mark   本页面 包括。邮箱 电话。绑定银行卡 优惠券。
- (void)viewDidLoad {
    [super viewDidLoad];
    self.information =  [NSString isNotNull:self.information];
    self.namein =  [NSString isNotNull:self.namein];
    [self displayViewContent];
    self.userNameTf.delegate = self;
    self.wecheAccountTf.delegate = self;
    self.phoneTf.delegate = self;
    self.viewModel = [[TMUserViewModel alloc]init];
    self.view.backgroundColor = [UIColor hx_colorWithHexRGBAString:@"#F7F7F7"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(choosePhoto:) name:@"CHOOSEPHOTOACTION" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(choosePhotoCram:) name:@"CHOOSEPHOTOACTIONCRAM" object:nil];
}
- (void)displayRightItem{
    UIBarButtonItem *letfItem = [[UIBarButtonItem alloc]initWithTitle:@"保存"  style:UIBarButtonItemStylePlain target:self action:@selector(addTeacher)];
    self.navigationItem.rightBarButtonItem = letfItem;
}
- (void)addTeacher{
    NSLog(@"保存成功");
    if ([self.vcTitle isEqualToString:@"昵称"]) {
        [self changeNickName];
    }
    if ([self.vcTitle isEqualToString:@"邮箱"]) {
        [self changeMail];
    }
}
- (void)displayViewContent{
    if ([self.vcTitle isEqualToString:@"头像"]) {
        self.title = @"个人头像";
        self.bindView.hidden = YES;
        self.discountCouponView.hidden = YES;
        self.phoneView.hidden = YES;
        self.imagev = [[UIImageView alloc]initWithFrame:CGRectMake(12, 53+64, kWindowW-24, 364*kWindowW/375)];
        self.imagev.contentMode = UIViewContentModeScaleAspectFill;
        self.imagev.clipsToBounds = YES;
        [self.imagev sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:self.information]] placeholderImage:kPlaceholder];
        [self.view addSubview:self.imagev];
        self.imagev.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeImageIcon:)];
        [self.imagev addGestureRecognizer:tap];
    }
    if ([self.vcTitle isEqualToString:@"昵称"]) {
        self.title = self.vcTitle;
        self.phoneView.hidden = NO;
        self.phoneTf.text = self.information;
        self.bindView.hidden = YES;
        self.discountCouponView.hidden = YES;
        self.phoneViewTopConst.constant = 13+64;
        [self displayRightItem];
    }
    if ([self.vcTitle isEqualToString:@"邮箱"]) {
        self.title = self.vcTitle;
        self.phoneTf.text = self.information;
        self.bindView.hidden = YES;
        self.discountCouponView.hidden = YES;
        self.phoneViewTopConst.constant = 13+64;
         [self displayRightItem];
    }
    if ([self.vcTitle isEqualToString:@"手机"]) {
//        self.title = self.vcTitle;
//        self.phoneTf.text = self.information;
//        self.bindView.hidden = YES;
//        self.discountCouponView.hidden = YES;
//        self.phoneViewTopConst.constant = 13+64;
//         [self displayRightItem];
        self.title = @"绑定手机号";
        self.userNameTf.placeholder = @"请输入手机号";
        self.wecheAccountTf.placeholder = @"请输入验证码";
//        self.userNameTf.text = self.information;
        //self.wecheAccountTf.text = self.information;
        self.bindView.hidden = NO;
        self.discountCouponView.hidden = YES;
        self.phoneView.hidden = YES;
        self.codeBtn.hidden = NO;

        UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(30, TMUtils.navgationBarBootom, 300, 40)];
        lable.textColor = UIColor.blackColor;
        lable.text = [NSString stringWithFormat:@"当前手机号码:%@",self.information];
        [self.view addSubview:lable];
        
    }
    if ([self.vcTitle isEqualToString:@"掌上生活"]) {
        self.title = @"绑定银行卡";
        self.userNameTf.placeholder = @"请输入姓名";
        self.wecheAccountTf.placeholder = @"请输入卡号";
        self.userNameTf.text = self.namein;
        self.wecheAccountTf.text = self.information;
        self.bindView.hidden = NO;
        self.discountCouponView.hidden = YES;
        self.phoneView.hidden = YES;
        self.codeBtn.hidden = YES;
    }
    if ([self.vcTitle isEqualToString:@"支付宝"]) {
        self.title = @"绑定支付宝账号";
        self.userNameTf.placeholder = @"请输入姓名";
        self.wecheAccountTf.placeholder = @"请输入支付宝账号";
        self.wecheAccountTf.keyboardType = UIKeyboardTypeASCIICapable;
        self.userNameTf.text = self.namein;
        self.wecheAccountTf.text = self.information;
        self.bindView.hidden = NO;
        self.discountCouponView.hidden = YES;
        self.phoneView.hidden = YES;
        self.codeBtn.hidden = YES;
        self.wecheAccountTf.autocorrectionType =  UITextAutocorrectionTypeNo;
    }
    if ([self.vcTitle isEqualToString:@"微信"]) {
        self.title = @"绑定微信账号";
        self.userNameTf.placeholder = @"请输入姓名";
        self.wecheAccountTf.placeholder = @"请输入微信账号";
        self.wecheAccountTf.keyboardType = UIKeyboardTypeASCIICapable;
        self.wecheAccountTf.autocorrectionType =  UITextAutocorrectionTypeNo;
        self.userNameTf.text = self.namein;
        self.wecheAccountTf.text = self.information;
        self.bindView.hidden = NO;
        self.discountCouponView.hidden = YES;
        self.phoneView.hidden = YES;
        self.codeBtn.hidden = YES;
    }
    if ([self.vcTitle isEqualToString:@"微博"]) {
        self.title = @"绑定微博账号";
        self.userNameTf.placeholder = @"请输入姓名";
        self.wecheAccountTf.placeholder = @"请输入微博账号";
        self.userNameTf.text = self.namein;
        self.wecheAccountTf.text = self.information;
        self.bindView.hidden = NO;
        self.discountCouponView.hidden = YES;
        self.phoneView.hidden = YES;
        self.codeBtn.hidden = YES;
    }
#pragma -mark 我的优惠券 拿出去 用表！！！！！
    if ([self.vcTitle isEqualToString:@"我的优惠券"]) {
        self.title = @"我的优惠券";
        self.bindView.hidden = YES;
        self.discountCouponView.hidden = NO;
        self.phoneView.hidden = YES;
        self.discountViewTopConst.constant = 13+64;
    }
}
- (IBAction)saveMessageAction:(UIButton *)sender {
    if ([self.vcTitle isEqualToString:@"掌上生活"]) {
        [self blindZSCard];
    }
    if ([self.vcTitle isEqualToString:@"支付宝"]) {
        [self blindZFB];
    }
    if ([self.vcTitle isEqualToString:@"微信"]) {
        [self blindWX];
    }
    if ([self.vcTitle isEqualToString:@"微博"]) {
        [self blindWB];
    }
    if ([self.vcTitle isEqualToString:@"手机"]) {
        [self changePhone];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return NO;
}
- (void)changeNickName{
    [self.viewModel changeNickName:self.phoneTf.text Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
- (void)changeMail{
    [self.viewModel changeMail:self.phoneTf.text Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
//限制输入数字！
- (BOOL)inputShouldNumber:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex =@"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}
- (void)changePhone{
    if (self.userNameTf.text.length != 11 || ![self inputShouldNumber:self.userNameTf.text]) {
        [TMProgressHUD showInfoWithStyleDarkStatus:@"请输入正确的手机号"];
        return;
    }
    if (self.wecheAccountTf.text == nil || ![self inputShouldNumber:self.wecheAccountTf.text]) {
        [TMProgressHUD showInfoWithStyleDarkStatus:@"请输入正确的验证码！"];
        return;
    }
    [self.viewModel changePhone:self.userNameTf.text AndCode:self.wecheAccountTf.text Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
- (void)blindZFB{
    [self.viewModel BlindZFB:self.userNameTf.text AndCard:self.wecheAccountTf.text Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
- (void)blindWX{
    [self.viewModel BlindWX:self.userNameTf.text AndCard:self.wecheAccountTf.text Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
- (void)blindWB{
    [self.viewModel BlindWB:self.userNameTf.text AndCard:self.wecheAccountTf.text Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
- (void)blindZSCard{
    [self.viewModel BlindZSCard:self.userNameTf.text AndCard:self.wecheAccountTf.text Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
- (void)changeIcon{
    [self.viewModel changeUserIcon:self.iconUrls Success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSString *error) {
        
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)changeImageIcon:(UITapGestureRecognizer *)taps{
    [TMChoicePhotosView choicePhotosAddToView:self.navigationController.view withType:ChoosePhotoIVLog];
}
- (void) openCameraOrLibraryWithSourceType:(UIImagePickerControllerSourceType) sourcetype{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = sourcetype;
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    [self presentViewController:imagePickerController animated:YES completion:^{
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage * originalImage = (UIImage *)info[UIImagePickerControllerEditedImage];
    if (_types == ChoosePhotoIVLog) {
        self.imagev.image = originalImage;
        [TMUserManager changeUserIcon:originalImage Success:^(id imageUrl) {
            self.iconUrls = imageUrl;
            [self changeIcon];
        }];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)choosePhoto:(NSNotification *)noti{
    [self openCameraOrLibraryWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)choosePhotoCram:(NSNotification *)noti{
    [self openCameraOrLibraryWithSourceType:UIImagePickerControllerSourceTypeCamera];
    //UIImagePickerControllerSourceTypeCamera
}
@end
